package com.emeraldparser.grammar;

import com.emeraldparser.symbol.SymbolClass;
import com.emeraldparser.symbol.SymbolClass.State;
import com.emeraldparser.util.HashCodes;
import com.google.common.base.Objects;
import com.google.common.base.Preconditions;

public final class LeftState implements State {
	
	private final Rule rule;
	private final SymbolClass.State leftState;
	private final transient int hashCode;

	public LeftState(Rule rule, State leftState) {
		this.rule = Preconditions.checkNotNull(rule, "Rule must not be null");
		this.leftState = Preconditions.checkNotNull(leftState, "Left state must not be null");
		this.hashCode = HashCodes.classSpecificHashCode(LeftState.class, rule, leftState);
	}

	public Rule getRule() {
		return rule;
	}

	public State getLeftState() {
		return leftState;
	}

	@Override
	public String toString() {
		return leftState + " = " + rule.getRightSymbolClass();
	}
	
	@Override
	public int hashCode() {
		return hashCode;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		
		if (obj == null) {
			return false;
		}
		
		if (getClass() != obj.getClass()) {
			return false;
		}
		
		LeftState other = (LeftState) obj;
		
		return Objects.equal(rule, other.rule)
		    && Objects.equal(leftState, other.leftState);
	}

}
